package com.dongdongshop.controller;

import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.CategoryService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("categoryController")
public class CategoryController {

    @DubboReference
    private CategoryService categoryService;

    //查询页面
    @RequestMapping("getCategoryList")
    @ResponseBody
    public AjaxResult<List<TbContentCategory>> getCategoryList(String name){
        try {
            List<TbContentCategory> list = categoryService.getCategoryList(name);
            return AjaxResult.OK().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //增加内容分类
    @RequestMapping("addCategory")
    @ResponseBody
    public AjaxResult addCategory(TbContentCategory contentCategory){
        if (contentCategory == null){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            categoryService.addCategory(contentCategory);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //回显内容信息
    @RequestMapping("editCategory")
    @ResponseBody
    public AjaxResult<TbContentCategory> editCategory(Long id){
        if (id == null){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            TbContentCategory contentCategory = categoryService.editCategory(id);
            return AjaxResult.OK().setData(contentCategory);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //修改内容分类
    @RequestMapping("updateCategory")
    @ResponseBody
    public AjaxResult updateCategory(TbContentCategory contentCategory){
        if (contentCategory == null){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            categoryService.updateCategory(contentCategory);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //删除内容分类
    @RequestMapping("deleteCategory")
    @ResponseBody
    public AjaxResult deleteCategory(Long [] ids){
        if (ids.length == 0){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            categoryService.deleteCategory(ids);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
